package com.blb.springboot.interceptor;

import com.blb.springboot.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author lvtian
 * @date 2023/1/10 16:24
 */
public class LoginInterceptor implements HandlerInterceptor {
    //前置处理
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //判断session是否有user对象，有就放行，没有就拦截
        User user = (User) request.getSession().getAttribute("user");
        if(user == null){
            //强制跳转登录
            response.sendRedirect("/toLogin");
            //返回 false 代表拦截
            return false;
        }
        //返回 true 代表放行
        return true;
    }
}
